﻿using System;
using System.Collections.Generic;
using System.Linq;
using Loki.Engine;
using Luna.Data.Accounting;
using NUnit.Framework;

namespace Luna.Data.Tests
{
    public class AccountingTest : AssertionHelper
    {
        public IAccountData Account1 { get; set; }
        public IAccountData Account2 { get; set; }
        public IAccountData Account3 { get; set; }

        private bool AreSame(IAccountData P_Account1, IAccountData P_Account2)
        {
            bool b = true;
            b &= P_Account1.PK == P_Account2.PK;
            b &= P_Account1.Name == P_Account2.Name;
            b &= P_Account1.Number == P_Account2.Number;
            b &= P_Account1.InitialAmount == P_Account2.InitialAmount;
            b &= P_Account1.StartDate == P_Account2.StartDate;
            b &= P_Account1.Bank == P_Account2.Bank;
            return b;
        }

        public IAccountingDataProvider Data { get; set; }

        private IAccountData Create(string P_Name)
        {
            IAccountData L_Return = Data.Create<IAccountData>();
            L_Return.PK = Guid.NewGuid();
            L_Return.Number = P_Name + "Number";
            L_Return.Name = P_Name + "Name";
            L_Return.Bank = P_Name + "Bank";
            L_Return.InitialAmount = P_Name.Length;
            L_Return.StartDate = DateTime.Today.AddDays(P_Name.Length);
            return L_Return;
        }

        [SetUp]
        public void SetUp()
        {
            Data = ContextHelper.GetDataModel<LunaDataModel>().Accounting;

            Account1 = Create("Account1");
            Account2 = Create("Account_2");
            Account3 = Create("Account___3");

            CleanTable();
        }

        private void CleanTable()
        {
            IEnumerable<IAccountData> L_Accounts = Data.GetAllAccounts();
            foreach (var L_Account in L_Accounts)
            {
                Data.DeleteAccount(L_Account.PK);
            }
        }

        [Test]
        public void TestInsertAndGet()
        {
            Data.SaveAccount(Account1);
            IAccountData L_Account = Data.GetAccount(Account1.PK);
            Expect(AreSame(Account1, L_Account), Is.True);
        }

        [Test]
        public void TestUpdateAndGet()
        {
            Data.SaveAccount(Account1);
            Account1.Name = "Account1_Name_Modified";
            Data.SaveAccount(Account1);

            IAccountData L_Account = Data.GetAccount(Account1.PK);
            Expect(AreSame(Account1, L_Account), Is.True);

            Expect(L_Account.Name, Is.EqualTo("Account1_Name_Modified"));
        }

        [Test]
        public void TestInsertAndGetAll()
        {
            Data.SaveAccount(Account1);
            Data.SaveAccount(Account2);
            Data.SaveAccount(Account3);

            int L_Count = Data.GetAllAccounts().Count();
            Expect(L_Count, Is.EqualTo(3));
        }

        [Test]
        public void TestInsertAndDelete()
        {
            Data.SaveAccount(Account1);
            Data.SaveAccount(Account2);
            Data.DeleteAccount(Account1.PK);

            int L_Count = Data.GetAllAccounts().Count();
            Expect(L_Count, Is.EqualTo(1));
            IAccountData L_Account = Data.GetAccount(Account1.PK);
            Expect(L_Account, Is.Null);
        }
    }
}
